# encoding=utf-8
from django.shortcuts import render
import requests
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from .setting import *

# Create your views here.


def index(request):
    """
        @author: 董海明
        @date: 10/16/2018
        为接口提供数据：
            提交的数据格式为：{"user":"xxx","pswd":"xxxxxx"}
    """
    if request.method == "POST":
        info = dict(request.POST)
        print(info)
        user, pswd = info["user_name"][0], info["pass_word"][0]
        login_api = url_ip + 'login/api_login/'
        print(login_api)
        r = requests.post(login_api, data={"user": user, "pswd": pswd})
        print(r.text)
        if "登陆成功" == r.text:
            return HttpResponseRedirect("https://www.baidu.com/")
        else:
            context = {}
            return render(request, "apilogin/index.html", context)
    elif request.method == "GET":
        context = {}
        return render(request, "apilogin/index.html", context)


def register(request):

    """
        @author:董海明
        @date: 10/16/2018
        本函数为接口提交数据：
            提交数据格式为：
                {'user_account': ['qwe'], 'user_name': ['qwesa'],
                'di_code': ['adsad'], 'phone':['asdsad'],
                'api_pswd': ['adsa'], 'sex': ['男']}
    """
    if request.method == 'POST':
        info = dict(request.POST)
        print(info)
        login_api = url_ip + 'login/api_register/'
        print(login_api)
        r = requests.post(login_api, data=info)
        if '注册成功' == r.text:
            return HttpResponseRedirect(reverse("apilogin:register"))
        else:
            return HttpResponseRedirect(reverse("apilogin:register"))
    elif request.method == 'GET':
        context = {}
        return render(request, 'apilogin/register.html', context)
    

def pswd(request):
    """
        @author: 董海明
        @date: 10/16/2018
        本函数为接口提交数据：
            提交数据格式为：
                {"user_name":xxx,"old_pswd":xxx,"new_pswd":xxx,"re_pswd":xxx}
    """
    if request.method == "POST":
        info = dict(request.POST)
        login_api = url_ip + 'login/api_pswd/'
        r = requests.post(login_api,data={"user_name": info["user_name"][0],
                                          "old_pswd": info["old_pswd"][0],
                                          "new_pswd": info["new_pswd"][0],
                                          "re_pswd": info["re_pswd"][0]})
        if "修改成功" == r.text:
            return HttpResponse("恭喜您！密码修改成功")
        else:
            return render(request, "apilogin/reg_pswd.html")
    elif request.method == "GET":
        return render(request, "apilogin/reg_pswd.html")
    










    

    
